from collections import OrderedDict

from django.shortcuts import render
from django.views import View

from meiduo_mall.apps.goods.models import GoodsChannel
from meiduo_mall.apps.index.models import ContentCategory

# Create your views here.


class IndexView(View):
    """首页广告界面"""
    def get(self, request):
        """
        {
            "1":{
                "channels":[
                    {"id":1, "name":"手机", "url":"http://shouji.jd.com/"},
                    {"id":2, "name":"相机", "url":"http://www.itcast.cn/"}
                ],
                "sub_cats":[
                    {
                        "id":38,
                        "name":"手机通讯",
                        "sub_cats":[
                            {"id":115, "name":"手机"},
                            {"id":116, "name":"游戏手机"}
                        ]
                    },
                    {
                        "id":39,
                        "name":"手机配件",
                        "sub_cats":[
                            {"id":119, "name":"手机壳"},
                            {"id":120, "name":"贴膜"}
                        ]
                    }
                ]
            },
            "2":{
                "channels":[],
                "sub_cats":[]
            }
        }
        """
        # 查询所有的商品频道
        categories = OrderedDict()
        channels = GoodsChannel.objects.order_by('group_id', 'sequence')
        # 遍历所有的频道
        for channel in channels:
            # 获取当前频道所在的组 (多查一)
            group_id = channel.group_id   # 或者group_id = channel.group_id
            if group_id not in categories:
                categories[group_id] = {'channels': [], 'sub_cats': []}

            cat1 = channel.category  # 当前频道的类别, 一个频道对应一个一级类别

            # 追加当前频道
            categories[group_id]['channels'].append({
                'id': cat1.id,
                'name': cat1.name,
                'url': channel.url
            })
            # 构建当前类别的子类别
            for cat2 in cat1.subs.all():
                cat2.sub_cats = []  # 给二级类别添加一个保存三级类别数据的列表
                for cat3 in cat2.subs.all():
                    cat2.sub_cats.append(cat3)
                categories[group_id]['sub_cats'].append(cat2)
        # print(categories)
        """
        OrderedDict([(1, {'channels': [{'id': 1, 'name': '手机', 'url': 'http://shouji.jd.com'}, 
        {'id': 2, 'name': '相机', 'url': 'http://www.itcast.cn'}, {'id': 3, 'name': '数码', 
        'url': 'http://www.itcast.cn'}], 'sub_cats': 
        [<GoodsCategory: 手机通讯>, <GoodsCategory: 手机配件>, <GoodsCategory: 摄影摄像>,
         <GoodsCategory: 数码配件>, <GoodsCategory: 影音娱乐>, <GoodsCategory: 智能设备>, 
         <GoodsCategory: 电子教育>]}),
        """

        # 查询首页广告数据
        contents = {}
        # 查询所有广告类别数据
        content_categories = ContentCategory.objects.all()
        for cat in content_categories:
            contents[cat.key] = cat.content_set.filter(status=True).order_by('sequence')

        # 渲染模板的上下文
        context = {
            'categories': categories,
            'contents': contents
        }
        return render(request, 'index.html', context)
